﻿using DevExpress.XtraEditors;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MikroMagazaObject
{
    public partial class frmEvrak : XtraForm
    {
        public EvrakTipleri EvrakTipi { get; set; }

        List<Hareket> _lines = new List<Hareket>();
        Mikro mikro = new Mikro();

        public frmEvrak()
        {
            InitializeComponent();
            for (int i = 0; i < 100; i++)
            {
                _lines.Add(new Hareket());
            }

            gridControl1.DataSource = _lines;
        }

        private void frmEvrak_Load(object sender, EventArgs e)
        {
            List<ComboItem> depolar = mikro.DepoListe();
            cmbCikisDepo.Properties.Items.AddRange(depolar);
            cmbGirisDepo.Properties.Items.AddRange(depolar);
            cmbSrm.Properties.Items.AddRange(mikro.SrmListe());
            cmbFirma.Properties.Items.AddRange(mikro.CariListe());
            dteTarih.DateTime = DateTime.Now;
            cmbCikisDepo.SelectedIndex = 0;
            cmbGirisDepo.SelectedIndex = 0;
            cmbSrm.SelectedIndex = 0;

            gridView1.Columns["StokKodu"].ColumnEdit = btnKodu;
            gridView1.Columns["StokAdi"].ColumnEdit = btnAdi;

            if (File.Exists(Application.StartupPath + "\\evrak.xml"))
            {
                gridView1.RestoreLayoutFromXml(Application.StartupPath + "\\evrak.xml");
            }
            txtSira.GotFocus += txtSira_GotFocus;
            txtSeri.Focus();

            if (EvrakTipi == EvrakTipleri.DepoSevk)
            {
                lblHedef.Text = "Hedef Depo";
                lblHedef.Visible = true;
                cmbGirisDepo.Visible = true;

                lblKaynak.Text = "Kaynak Depo";

                this.Text = "Depo Sevk İşlemi";
            }
            else if (EvrakTipi == EvrakTipleri.AlisIrsaliyesi)
                this.Text = "Toptan Alış İrsaliyesi";
            else if (EvrakTipi == EvrakTipleri.VerilenSiparis)
                this.Text = "Firmaya Verilen Sipariş";
        }

        void txtSira_GotFocus(object sender, EventArgs e)
        {
            txtSeri.Text = txtSeri.Text.ToUpper();
            txtSira.Text = mikro.MaxEvrakNo(EvrakTipi, txtSeri.Text, 0).ToString();
        }

        private void btnKodu_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            StokListe(frmListe.ListeTipleri.StokKoduAra);
        }

        private void StokListe(frmListe.ListeTipleri kod)
        {
            frmListe frm = new frmListe();
            frm.ListeTipi = kod;
            gridView1.PostEditor();

            if (kod == frmListe.ListeTipleri.StokKoduAra)
                frm.Filtre = gridView1.GetFocusedRowCellDisplayText(gridView1.Columns["StokKodu"]);
            else
                frm.Filtre = gridView1.GetFocusedRowCellDisplayText(gridView1.Columns["StokAdi"]);


            if (frm.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                Dictionary<string, string> vals = frm.Values;
                if (vals.Count > 0)
                {
                    gridView1.SetFocusedRowCellValue(gridView1.Columns["StokKodu"], vals["StokKodu"]);
                    gridView1.SetFocusedRowCellValue(gridView1.Columns["StokAdi"], vals["StokAdı"]);
                    gridView1.SetFocusedRowCellValue(gridView1.Columns["KdvOrani"], vals["KdvOrani"]);
                    gridView1.SetFocusedRowCellValue(gridView1.Columns["KdvSira"], vals["KdvSira"]);
                }
            }
        }

        private void btnAdi_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            StokListe(frmListe.ListeTipleri.StokAdiAra);
        }

        private void btnKodu_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.F10)
            {
                StokListe(frmListe.ListeTipleri.StokKoduAra);
            }
        }

        private void btnAdi_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.F10)
            {
                StokListe(frmListe.ListeTipleri.StokAdiAra);
            }
        }

        private void gridControl1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.F10)
            {
                if (gridView1.FocusedColumn == gridView1.Columns["StokKodu"])
                {
                    StokListe(frmListe.ListeTipleri.StokKoduAra);
                }
                else if (gridView1.FocusedColumn == gridView1.Columns["StokAdi"])
                {
                    StokListe(frmListe.ListeTipleri.StokAdiAra);
                }
            }
        }

        private void cmbFirma_Properties_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            if (e.Button.Index == 1)
            {
                CariListele();
            }
        }

        private void CariListele()
        {
            frmListe frm = new frmListe();
            frm.ListeTipi = frmListe.ListeTipleri.Cari;

            if (frm.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                Dictionary<string, string> vals = frm.Values;
                if (vals.Count > 0)
                {
                    for (int i = 0; i < cmbFirma.Properties.Items.Count; i++)
                    {
                        if (cmbFirma.Properties.Items[i].ToString() == vals["Text"])
                        {
                            cmbFirma.SelectedIndex = i;
                            break;
                        }
                    }
                }
            }
        }

        private void frmEvrak_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (File.Exists(Application.StartupPath + "\\evrak.xml"))
            {
                File.Delete(Application.StartupPath + "\\evrak.xml");
            }
            gridView1.SaveLayoutToXml(Application.StartupPath + "\\evrak.xml");
        }

        private void CalcSubTotal()
        {
            decimal aratop = 0;
            decimal isktop = 0;
            decimal kdvtop = 0;
            decimal geneltop = 0;

            foreach (Hareket item in _lines)
            {
                aratop += item.Tutari;
                isktop += item.Tutari - item.NetTutar;
                kdvtop += item.KdvTutari;
                geneltop += item.Tutari - (item.Tutari - item.NetTutar) + item.KdvTutari;
            }

            txtAratop.Text = aratop.ToString("n2");
            txtIsk.Text = isktop.ToString("n2");
            txtKdv.Text = kdvtop.ToString("n2");
            txtMeblag.Text = geneltop.ToString("n2");
        }

        private void gridView1_CellValueChanged(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
        {
            CalcSubTotal();
        }

        private void cmbFirma_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.F10)
            {
                CariListele();
            }
        }

        private void simpleButton2_Click(object sender, EventArgs e)
        {
            DialogResult rs = MessageBox.Show("Evrağı kaydetmek istediğinize eminmisiniz ?", "Dikkat", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (rs == System.Windows.Forms.DialogResult.Yes)
            {
                string carikodu = ((ComboItem)cmbFirma.SelectedItem).Value;
                string srmkodu = ((ComboItem)cmbSrm.SelectedItem).Value;
                int girisdepo = Cevir.Parse<int>(((ComboItem)cmbGirisDepo.SelectedItem).Value);
                int cikisdepo = Cevir.Parse<int>(((ComboItem)cmbCikisDepo.SelectedItem).Value);

                mikro.EvrakKaydet(EvrakTipi, txtSeri.Text, Cevir.Parse<int>(txtSira.Text), dteTarih.DateTime, carikodu, txtBelgeNo.Text, srmkodu, girisdepo, cikisdepo, _lines);

                MessageBox.Show("Kaydedildi !");
                this.Close();
            }
        }

        private void txtSira_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            EvrakListe();
        }

        private void EvrakListe()
        {
            frmListe frm = new frmListe();
            switch (EvrakTipi)
            {
                case EvrakTipleri.AlisIrsaliyesi:
                    frm.ListeTipi = frmListe.ListeTipleri.IrsaliyeListe;
                    break;
                case EvrakTipleri.DepoSevk:
                    frm.ListeTipi = frmListe.ListeTipleri.DepoSevkListe;
                    break;
                case EvrakTipleri.VerilenSiparis:
                    frm.ListeTipi = frmListe.ListeTipleri.SiparisListe;
                    break;
            }


            if (frm.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                Dictionary<string, string> vals = frm.Values;
                if (vals.Count > 0)
                {
                    Evrak evr = mikro.EvrakYukle(EvrakTipi, vals["Seri"], Cevir.Parse<int>(vals["Sıra"]));

                    for (int i = 0; i < cmbCikisDepo.Properties.Items.Count; i++)
                    {
                        if (((ComboItem)cmbCikisDepo.Properties.Items[i]).Value == evr.CikisDepo.ToString())
                        {
                            cmbCikisDepo.SelectedIndex = i;
                            break;
                        }
                    }

                    for (int i = 0; i < cmbGirisDepo.Properties.Items.Count; i++)
                    {
                        if (((ComboItem)cmbGirisDepo.Properties.Items[i]).Value == evr.GirisDepo.ToString())
                        {
                            cmbGirisDepo.SelectedIndex = i;
                            break;
                        }
                    }

                    for (int i = 0; i < cmbFirma.Properties.Items.Count; i++)
                    {
                        if (((ComboItem)cmbFirma.Properties.Items[i]).Value == evr.CariKodu)
                        {
                            cmbFirma.SelectedIndex = i;
                            break;
                        }
                    }

                    dteTarih.DateTime = evr.Tarih;
                    txtBelgeNo.Text = evr.BelgeNo;
                    txtSeri.Text = vals["Seri"];
                    txtSira.Text = vals["Sıra"];

                    _lines.Clear();
                    _lines.AddRange(evr.Lines);
                    for (int i = 0; i < 100; i++)
                    {
                        _lines.Add(new Hareket());
                    }

                    gridView1.RefreshData();
                    CalcSubTotal();

                }
            }
        }

        private void txtSira_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.F10)
            {
                EvrakListe();
            }
        }
    }
}
